package com.wxj.test;

public class Utils {

    // 地球半径，单位为米
    private static final double EARTH_RADIUS_METERS = 6378137;

    // 计算两点之间的距离
    public static double haversine(Point p1, Point p2) {
        final int R = 6371; // 地球半径，单位为公里

        // 将经纬度转换为弧度
        double latDistance = Math.toRadians(p2.getLatitude() - p1.getLatitude());
        double lonDistance = Math.toRadians(p2.getLongitude() - p1.getLongitude());

        // 计算 Haversine 公式
        double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) +
                Math.cos(Math.toRadians(p1.getLatitude())) * Math.cos(Math.toRadians(p2.getLatitude())) *
                        Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        return R * c; // 返回距离
    }

    // 检查某个点是否在指定的区域内
    public static boolean isInArea(Point center, Point point, double radius) {
        double distance = haversine(center, point);
        System.out.println("distance = " + distance);
        return distance <= radius; // 如果距离小于等于半径，则返回true
    }

    public static void main(String[] args) {
        Point center = new Point(30.5, 114.5); // 中心点
        double radius = 10.0; // 半径，单位为公里

        // 测试点
        Point[] points = {
                new Point(30.6, 114.5),
                new Point(30.0, 114.0),
                new Point(30.7, 114.6),
                new Point(30.3, 114.4)
        };

        for (Point point : points) {
            if (isInArea(center, point, radius)) {
                System.out.println("Point (" + point.getLatitude() + ", " + point.getLongitude() + ") is within the area.");
            } else {
                System.out.println("Point (" + point.getLatitude() + ", " + point.getLongitude() + ") is outside the area.");
            }
        }
    }
}
